package test;

public class test01 {

    public static void main(String[] args) {
        int[] num1 = {0,2};
        int[] num2 = {4};
        double middle = findMedianSortedArrays(num1, num2);
        System.out.println(middle);
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        if(nums1.length == 0 && nums2.length == 0){
            return 0.0;
        }
        int i = 0;
        int j = 0;
        int index = 0;
        int temp = 0;
        int temp2 = 0;
        int length = nums1.length + nums2.length;
        int middle = (length-1)/2;

        while (i < nums1.length && j < nums2.length && index < middle){
            if(nums1[i] <= nums2[j]){
                index++;
            }else{
                index++;
            }
        }
        if(index == middle && i < nums1.length && j < nums2.length ){
            if(nums1[i] < nums2[j]){
                temp = nums1[i++];
                if(nums1[i] < nums2[j]){
                   temp2 =  nums1[i];
                }else{
                    temp2 =  nums2[j];
                }
            }else{
                temp = nums2[j++];
                if(nums1[i] < nums2[j]){
                    temp2 =  nums1[i];
                }else{
                    temp2 =  nums2[j];
                }
            }
        }
        if(i == nums1.length && index < middle){
            for (int k = j; k < nums2.length; k++) {
                if(index == middle){
                    temp = nums2[k++];
                    temp2 = nums2[k];
                }
            }
        }
        if(j == nums2.length && index < middle){
            for (int k = i; k < nums1.length; k++) {
                if(index == middle){
                    temp = nums1[k++];
                    temp2 = nums1[k];
                }
            }
        }
        return (length%2!=0)?temp:((temp+temp2)/2.0);
    }
}
